#include "scene.h"
#include "tinyxml/tinyxml.h"

void Scene_C::Init(int id,const char* filename)
{
	m_ID = id;
	m_OList.clear();

	TiXmlDocument doc;
	TiXmlElement  *top,*ele;
	void *buf;
	int type;
	float x,y,w,h,a;

	buf = hge->Resource_Load(filename);
	doc.Parse((char*)buf);

	top = doc.RootElement();
	top = top->FirstChildElement("scene");
	if( top )
	{
		for( ele=top->FirstChildElement("object"); ele; ele=ele->NextSiblingElement("object") )
		{
			ele->QueryIntAttribute("type",&type);
			ele->QueryFloatAttribute("x",&x);
			ele->QueryFloatAttribute("y",&y);
			ele->QueryFloatAttribute("w",&w);
			ele->QueryFloatAttribute("h",&h);
			if( type == 0 )
			{
				m_OList.push_back(new Floor_C(x,y,w,h));
			}
			else if( type == 1 )
			{
				ele->QueryFloatAttribute("a",&a);
				m_OList.push_back(new Chacha_C(x,y,w,h,a));
			}
			else if( type == 2 )
			{
				float speed,yend;
				ele->QueryFloatAttribute("speed",&speed);
				ele->QueryFloatAttribute("yend",&yend);
				m_OList.push_back(new DropRake(x,y,w,h,speed,yend));
			}
		}
	}
}

bool Scene_C::Update(float dt,Role_C &role)
{
	ObjectList::iterator it = m_OList.begin();
	for( ; it != m_OList.end(); it++ )
	{
		(*it)->Update(dt,role);
	}

	if( role.m_Rect.x > SCREEN_WIDTH-role.m_Rect.w/2 )
		return true;
	else
		return false;
}

void Scene_C::Draw(float x,float y)
{
	ObjectList::iterator it = m_OList.begin();
	for( ; it != m_OList.end(); it++ )
	{
		(*it)->Draw(x,y);
	}
}

void Scene_C::Exit()
{
	ObjectList::iterator it = m_OList.begin();
	for( ; it != m_OList.end(); it++ )
	{
		delete (*it);
	}
	m_OList.clear();
}